package es.ucm.fdi.healthpred.controlador.utils.trace;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Properties;

/**
 * Utilidad para generar trazas en archivos y/o salida est&aacute;ndar, error,
 * etc. mediante el patr&oacute;n <i>Singleton</i>.
 * 
 * @since 1.0
 * 
 * @version 1.0
 * 
 * @author &Aacute;lvaro G&oacute;mez Porris
 * @author V&iacute;ctor L&oacute;pez Monte
 * @author Carlos Rodr&iacute;guez Colliga
 * 
 */
public class SingletonTrace {

	public static final boolean _traceOn = false;
	public static final boolean _useTraceBBDD = false;
	private final static int _traceDestination = ITFConstansTrace.CONSOLA_TRACE;
	private static SingletonTrace _uniqueInstance = null;

	/**
	 * @return La instancia &uacute;nica.
	 */
	public static SingletonTrace getInstance() {
		if (_uniqueInstance == null) {
			_uniqueInstance = new SingletonTrace();
		}
		return _uniqueInstance;
	}

	public String directory = null;
	public String fileTrace = null;
	private Properties connectionProperties;
	private InputStream fileproperties;;

	private SingletonTrace() {
		try {
			fileproperties = SingletonTrace.class
					.getResourceAsStream(ITFConstansTrace.TRACE_CONFIGURATION);
			connectionProperties = new Properties();
			connectionProperties.load(fileproperties);
			fileTrace = String.valueOf(connectionProperties
					.getProperty(ITFConstansTrace.FILETRACE));
			directory = String.valueOf(connectionProperties
					.getProperty(ITFConstansTrace.DIRECTORY));
			File file = new File(directory);
			file.mkdir();
		} catch (Exception ex) {
			System.out.print(ITFConstansTrace.ERROR + ex.getMessage());
		}
	}

	/**
	 * @param p_errorTrace
	 *            Traza de entrada (cadena).
	 * @return Traza de error formateada (Y/M/D(H:M:S)p_errorTrace)
	 */
	public String formatErrorTrace(String p_errorTrace) {
		StringBuffer errorMessage = new StringBuffer();
		Calendar currentDate = null;
		currentDate = Calendar.getInstance();
		errorMessage.append(currentDate.get(Calendar.YEAR))
				.append(ITFConstansTrace.SLASH)
				.append((currentDate.get(Calendar.MONTH) + 1))
				.append(ITFConstansTrace.SLASH)
				.append(currentDate.get(Calendar.DAY_OF_MONTH))
				.append(ITFConstansTrace.PARENTESIS_ABRIR)
				.append(currentDate.get(Calendar.HOUR_OF_DAY))
				.append(ITFConstansTrace.DOS_PUNTOS)
				.append(currentDate.get(Calendar.MINUTE))
				.append(ITFConstansTrace.DOS_PUNTOS)
				.append(currentDate.get(Calendar.SECOND))
				.append(ITFConstansTrace.PARENTESIS_CERRRAR)
				.append(p_errorTrace);
		return errorMessage.toString();
	}

	/**
	 * @param p_text
	 *            Traza de entrada (cadena).
	 * @return Traza formateada<br>
	 *         <ul>
	 *         <li>Fecha: p_text (en archivo).</li>
	 *         <li>Fecha: &lt;HealthPred 1.0&gt; p_text (por consola).</li>
	 *         </ul>
	 */
	public int writeTrace(String p_text) {
		try {
			SingletonDateGenerator date = SingletonDateGenerator.getInstance();
			switch (_traceDestination) {
			case 1:
				this.writeFile(date.getcurrentDate() + ": " + p_text);
				break;
			case 2:
				System.out.println(date.getcurrentDate()
						+ " : <HealthPred 1.0> " + p_text);
				break;
			default:
				break;
			}
			return 0;
		} catch (Exception ex) {
			System.out.print(ITFConstansTrace.ERROR + ex.getMessage());
			return -1;
		}
	}

	private synchronized void writeFile(String p_text) {
		try {
			FileWriter file = new FileWriter(fileTrace, true);
			file.write(p_text + ITFConstansTrace.NUEVA_LINEA);
			file.flush();
			file.close();
		} catch (IOException ex) {
			System.out.print(ITFConstansTrace.ERROR + ex.getMessage());
		}
	}
}